import java.util.Scanner;

public class CourseSystem {
    // 使用数组存储数据
    private Course[] courses;
    private Teacher[] teachers;
    private Student[] students;

    private int courseCount;
    private int teacherCount;
    private int studentCount;

    private Scanner scanner;

    // 构造方法
    public CourseSystem() {
        courses = new Course[20];  // 最多20门课程
        teachers = new Teacher[10]; // 最多10名教师
        students = new Student[20]; // 最多20名学生

        courseCount = 0;
        teacherCount = 0;
        studentCount = 0;

        scanner = new Scanner(System.in);

        // 初始化一些示例数据
        initSampleData();
    }

    // 初始化示例数据
    private void initSampleData() {
        // 添加示例教师
        addTeacher("T1", "wsx", "女");
        addTeacher("T2", "yym", "男");

        // 添加示例课程
        addCourse("C1", "Java", "2501", "周四 8:00", 3);
        addCourse("C2", "高等数学", "2501", "周五 13:30", 2);

        // 添加示例学生
        addStudent("S1", "LZB", "男");
        addStudent("S2", "SKY", "男");

        // 教师开课
        assignCourseToTeacher("T1", "C1");
        assignCourseToTeacher("T2", "C2");
    }

    // 添加课程
    private void addCourse(String id, String name, String location, String time, int maxStudents) {
        if (courseCount < courses.length) {
            courses[courseCount] = new Course(id, name, location, time, maxStudents);
            courseCount++;
        }
    }

    // 添加教师
    private void addTeacher(String id, String name, String gender) {
        if (teacherCount < teachers.length) {
            teachers[teacherCount] = new Teacher(id, name, gender);
            teacherCount++;
        }
    }

    // 添加学生
    private void addStudent(String id, String name, String gender) {
        if (studentCount < students.length) {
            students[studentCount] = new Student(id, name, gender);
            studentCount++;
        }
    }

    // 教师开课
    private void assignCourseToTeacher(String teacherId, String courseId) {
        Teacher teacher = findTeacher(teacherId);
        Course course = findCourse(courseId);

        if (teacher != null && course != null) {
            teacher.courseId = courseId;
            course.teacherName = teacher.name;
        }
    }

    // 查找课程
    private Course findCourse(String courseId) {
        for (int i = 0; i < courseCount; i++) {
            if (courses[i].id.equals(courseId)) {
                return courses[i];
            }
        }
        return null;
    }

    // 查找教师
    private Teacher findTeacher(String teacherId) {
        for (int i = 0; i < teacherCount; i++) {
            if (teachers[i].id.equals(teacherId)) {
                return teachers[i];
            }
        }
        return null;
    }

    // 查找学生
    private Student findStudent(String studentId) {
        for (int i = 0; i < studentCount; i++) {
            if (students[i].id.equals(studentId)) {
                return students[i];
            }
        }
        return null;
    }

    // 显示所有课程
    public void showAllCourses() {
        System.out.println("\n=== 所有课程 ===");
        if (courseCount == 0) {
            System.out.println("暂无课程");
        } else {
            for (int i = 0; i < courseCount; i++) {
                courses[i].showInfo();
            }
        }
    }

    // 显示学生列表（简略版）
    private void showStudentList() {
        System.out.println("\n学生列表:");
        for (int i = 0; i < studentCount; i++) {
            System.out.println(students[i].id + " - " + students[i].name);
        }
    }

    // 学生选课
    public void studentSelectCourse() {
        System.out.println("\n--- 学生选课 ---");

        showStudentList();
        System.out.print("请输入学生编号: ");
        String studentId = scanner.nextLine();

        Student student = findStudent(studentId);
        if (student == null) {
            System.out.println("学生不存在！");
            return;
        }

        showAllCourses();
        System.out.print("请输入课程编号: ");
        String courseId = scanner.nextLine();

        Course course = findCourse(courseId);
        if (course == null) {
            System.out.println("课程不存在！");
            return;
        }

        if (course.isFull()) {
            System.out.println("课程已满，无法选择！");
            return;
        }

        // 检查是否已经选了这门课
        for (int i = 0; i < student.courseCount; i++) {
            if (student.courseIds[i].equals(courseId)) {
                System.out.println("已经选择了这门课程！");
                return;
            }
        }

        // 选课
        if (student.courseCount < student.courseIds.length) {
            student.courseIds[student.courseCount] = courseId;
            student.courseCount++;
            course.currentStudents++;
            System.out.println(student.name + " 成功选择了课程: " + course.name);
        } else {
            System.out.println("选课数量已达上限！");
        }
    }

    // 学生退课
    public void studentDropCourse() {
        System.out.println("\n--- 学生退课 ---");

        showStudentList();
        System.out.print("请输入学生编号: ");
        String studentId = scanner.nextLine();

        Student student = findStudent(studentId);
        if (student == null) {
            System.out.println("学生不存在！");
            return;
        }

        if (student.courseCount == 0) {
            System.out.println("该学生没有选择任何课程！");
            return;
        }

        // 显示学生已选课程
        System.out.println(student.name + " 已选课程:");
        for (int i = 0; i < student.courseCount; i++) {
            Course course = findCourse(student.courseIds[i]);
            if (course != null) {
                System.out.println((i + 1) + ". " + course.name + " (" + course.id + ")");
            }
        }

        System.out.print("请输入要退选的课程编号: ");
        String courseId = scanner.nextLine();

        // 查找课程在数组中的位置
        int index = -1;
        for (int i = 0; i < student.courseCount; i++) {
            if (student.courseIds[i].equals(courseId)) {
                index = i;
                break;
            }
        }

        if (index == -1) {
            System.out.println("该学生没有选择这门课程！");
            return;
        }

        // 退课
        Course course = findCourse(courseId);
        if (course != null) {
            course.currentStudents--;
        }

        // 从数组中移除课程
        for (int i = index; i < student.courseCount - 1; i++) {
            student.courseIds[i] = student.courseIds[i + 1];
        }
        student.courseCount--;

        System.out.println(student.name + " 成功退选了课程: " + (course != null ? course.name : courseId));
    }

    // 显示学生课表
    public void showStudentSchedule() {
        System.out.println("\n--- 查看学生课表 ---");

        showStudentList();
        System.out.print("请输入学生编号: ");
        String studentId = scanner.nextLine();

        Student student = findStudent(studentId);
        if (student == null) {
            System.out.println("学生不存在！");
            return;
        }

        System.out.println("\n=== " + student.name + " 的课表 ===");
        System.out.println("学号: " + student.id);

        if (student.courseCount == 0) {
            System.out.println("当前未选择任何课程");
        } else {
            System.out.println("编号\t课程名称\t上课地点\t上课时间\t授课教师");
            System.out.println("----\t--------\t--------\t--------\t--------");

            for (int i = 0; i < student.courseCount; i++) {
                Course course = findCourse(student.courseIds[i]);
                if (course != null) {
                    System.out.printf("%s\t%-8s\t%-8s\t%-8s\t%-8s\n",
                            course.id, course.name, course.location, course.time, course.teacherName);
                }
            }
        }
        System.out.println("=========================");
    }

    // 显示主菜单
    public void showMenu() {
        System.out.println("\n=== 学生选课系统 ===");
        System.out.println("1. 查看所有课程");
        System.out.println("2. 学生选课");
        System.out.println("3. 学生退课");
        System.out.println("4. 查看学生课表");
        System.out.println("0. 退出系统");
        System.out.print("请选择操作: ");
    }

    // 运行系统
    public void run() {
        System.out.println("欢迎使用学生选课系统！");

        while (true) {
            showMenu();
            String choice = scanner.nextLine();

            switch (choice) {
                case "1":
                    showAllCourses();
                    break;
                case "2":
                    studentSelectCourse();
                    break;
                case "3":
                    studentDropCourse();
                    break;
                case "4":
                    showStudentSchedule();
                    break;
                case "0":
                    System.out.println("感谢使用选课系统，再见！");
                    return;
                default:
                    System.out.println("无效选择，请重新输入！");
            }

            // 暂停等待用户按键
            System.out.print("按回车键继续...");
            scanner.nextLine();
        }
    }

    // 主方法
    public static void main(String[] args) {
        CourseSystem system = new CourseSystem();
        system.run();
    }
}